<?php

namespace app\admin\controller;

use app\admin\model\Specification;
use app\common\controller\Backend;
use think\Db;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Productslist extends Backend
{

    /**
     * Productslist模型对象
     * @var \app\admin\model\Productslist
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Productslist;

        $this->assign('series', Db::table('fa_series')->select());
        $this->assign('textures', Db::table('fa_texture')->select());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index()
    {
        $cid = input('ids');

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->where('productstype',$cid)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $row) {

            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->assignconfig('cid', $cid);
        return $this->view->fetch();
    }




    public function add($ids = null)
    {
        
        $list = Db::name('productstype')->where('id',$ids)->find();
        
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {
               $result = $this->model->save($params);
                if ($result !== false) {
                    $specifications = $this->request->post("specifications/a");
                    if (!empty($specifications)) {
                        foreach ($specifications as $spec) {
                            $spec['product_id'] = $this->model->getLastInsID();
                            Specification::create($spec);
                        }
                    }

                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->assign('row', $list);
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $product = \app\admin\model\Productslist::get($ids, ['specifications']);
        if (!$product) {
            $this->error(__('No Results were found'));
        }

        if ($this->request->isPost()) {
            $params['shswitch'] = 0;
            $params = $this->request->post("row/a");
            $specifications = $this->request->post("specifications/a");

            if ($params) {
                $result = $product->save($params);
                if ($result !== false) {
                    // Delete old specifications
                    Specification::where('product_id', $product->id)->delete();
                    // Save new specifications
                    if (!empty($specifications)) {
                        foreach ($specifications as $spec) {
                            $spec['product_id'] = $product->id;
                            Specification::create($spec);
                        }
                    }

                    // 生成前台预览页面的 URL，假设根据记录 ID 生成
                    $previewUrl = url('/index/products/show', ['id' => $product->id, 'preview' => 1]);
                    $previewUrl = str_replace('/dashboard.php', '', $previewUrl);
                    $this->success('', '', ['url' => $previewUrl]);
                } else {
                    $this->error($product->getError());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $this->view->assign("row", $product);
        return $this->view->fetch();
    }

}
